<template>
	<view class="left bai clearfix">
		<view class="menu-banner left clearfix" style="height:50px;margin-bottom:20px;"  :style=" focus == 'goods_category'?'border:1px solid red':'border:none;'">
			<button style="background:#2979ff;color:#fff;font-size:13px;height:40px;line-height:40px;" @click="translation()">一键翻译中文至英文</button>
		</view>
		
		<view class="menu-banner left clearfix" style="height:70px;margin-bottom:20px;"  :style=" focus == 'goods_category'?'border:1px solid red':'border:none;'" id="goods_category">
			<view class="goods-fenlei-title left" style="margin-top:-10px;font-size:13px;font-weight:500;">
				产品分类(Product Category)
			</view>
			<view class="goods-fenlei-content left">
				<uni-data-picker placeholder="请选择分类" v-model="categoryVal" popup-title="请选择商品分类" :localdata="category" @change="onchangeCategory">
				</uni-data-picker>
			</view>
		</view>
		
		<view class="menu-banner left clearfix" style="height:70px;margin-bottom:20px;"  :style=" focus == 'goods_title'?'border:1px solid red':'border:none;'" id="goods_title">
			<view class="goods-fenlei-title left" style="margin-top:-10px;font-size:13px;font-weight:500;">
				产品标题(Product Title)
			</view>
			<view class="goods-fenlei-content left">
				<input placeholder="Please enter the product title within 50 characters" v-model="formData.en_title" :focus="focus=='goods_title'?true:false" style="border:1px solid #eee;height:40px;border-radius: 5px;width:calc(100% - 10px);padding-left:10px;" />
			</view>
		</view>
		
		
		<view class="menu-banner left clearfix" style="height:350px;margin-bottom:20px;"  :style=" focus == 'goods_image'?'border:1px solid red':'border:none;'" id="goods_image">
			<view class="goods-fenlei-title left" style="margin-top:-10px;font-size:13px;font-weight:500;">
				产品主图(Product main image)
			</view>
			<view class="goods-fenlei-content left">
				<view class="goods-image-left left clearfix" style="padding:5px;width:100%;">
					<zmt-multiple-upload-images :value="formData.images" @change="uploadZhuImages" :max-count="1"></zmt-multiple-upload-images>
				</view>
			</view>
		</view>
		
		
		<!--主图开始-->
		<view class="menu-banner left clearfix" :style=" focus == 'goods_poster'?'border:1px solid red':'border:none;'" style="height:300px;" id="goods_poster">
			<zmt-upload-image @success="uploadBannerImage" :url="formData.en_poster"></zmt-upload-image>
		</view>
		
		
		
		
		
		<view class="label-box left clearfix">
			<zmt-en-label @content-change="handleContentChange" :content="[{html:formData.en_spec},{html:formData.en_describe},{html:formData.en_example_image}]"></zmt-en-label>
		</view>
		
		<view class="left bai" style="width:1240px;margin-left:calc((100% - 1040px)/2);margin-top:30px;font-weight:bold;">
			销售商品
		</view>
		
		<!--商品主图和介绍信息-->
		<view class="goods-box left clearfix relative" v-for="(item,index) in param">
			<!--删除按钮-->
			<!-- <view class="goods-param-del absolute" @click="removeParam(index)" v-if="i != 0">
				<uni-icons type="trash" color="#666" size="25"></uni-icons>
			</view> -->
			<!--标题开始-->
			<view class="goods-title left clearfix">
				<input placeholder="Please enter a title" v-model="param[index].en_title"/>
			</view>
			
			<view class="goods-image left clearfix">
				
				<view class="goods-image-left left clearfix" style="padding:5px;">
					<zmt-upload-images :number="1" :index="index" @success="uploadZhuImage" :url="param[index] && param[index].en_goods_image && param[index].en_goods_image[0]?param[index].en_goods_image[0]:''"></zmt-upload-images>
				</view>
				
				<view class="goods-image-content left clearfix">
					<view class="goods-image-top left clearfix" style="margin-top:5px;">
						<textarea v-model="param[index].en_feature" maxlength="-1" placeholder="Please enter the product features, and use | to separate each item, such as feature 1 | feature 2 | feature 3. You can also enter | and press Enter to wrap the line"></textarea>
					</view>
					
					<view class="goods-image-top left clearfix" style="height:150px;margin-top:20px;">
						<textarea v-model="param[index].en_content" maxlength="-1" placeholder="Please enter a brief introduction···"></textarea>
					</view>
					
				</view>
				
				<view class="goods-image-left right clearfix">
					<zmt-upload-images :number="1" :index="index"  :url="param[index].en_goods_example_image" title="请上传应用示意图(800px * 800px)" @success="uploadExampleImage"></zmt-upload-images>
				</view>
				
			</view>
			
			
			<view class="goods-param-content left clearfix">
				
				<view class="left clearfix bai relative" v-for="(items,i) in param[index].sku" style="margin-top:20px;">
					<!--删除按钮-->
					<!-- <view class="goods-param-del absolute" @click="removeSku(index,i)" v-if="i != 0">
						<uni-icons type="closeempty" color="#666" size="25"></uni-icons>
					</view> -->
				<!--属性开始-->
				<uni-table border stripe emptyText="暂无更多数据">
					<!-- 表头行 -->
					<uni-tr>
						<uni-th align="center">型号</uni-th>
						<uni-th align="center">编号</uni-th>
						<uni-th align="center">规格</uni-th>
						<uni-th align="center" style="width:230px;">图档</uni-th>
						<uni-th align="center" style="width:270px;">发货周期</uni-th>
						<uni-th align="center">单价</uni-th>
					</uni-tr>
					<!-- 表格数据行 -->
					<uni-tr>
						<uni-td>
							<input placeholder="Please enter the model number" v-model="param[index].sku[i].en_model" />
						</uni-td>
						<uni-td>
							<input placeholder="Please enter the number" v-model="param[index].sku[i].en_serial_number"/>
						</uni-td>
						<uni-td><input placeholder="Please enter the specifications" v-model="param[index].sku[i].en_spec"/></uni-td>
						<uni-td>
							<view class="param-upload-box left" v-if="!param[index].sku || !param[index].sku[i] || !param[index].sku[i].en_zh_pdf  || !param[index].sku[i].en_zh_pdf.url" @click="uploadFile('zhpdf',index,i)">
								上传PDF文档
							</view>
							
							<view class="param-upload-box left" style="background:rgb(38, 153, 57)" v-else @click="uploadFile('zhpdf',index,i)">
								已上传
							</view>
							
							
							
							<view class="param-upload-box left" v-if="!param[index].sku || !param[index].sku[i] || !param[index].sku[i].en_zh_3d || !param[index].sku[i].en_zh_3d.url" @click="uploadFile('zh3d',index,i)">
								上传3D文档
							</view>
							
							<view class="param-upload-box left" style="background:rgb(38, 153, 57)" v-else @click="uploadFile('zh3d',index,i)">
								已上传
							</view>
							
							
						</uni-td>
						<uni-td>
							<label class="goods-param-time left" :style="dItem.value == param[index].sku[i].en_delivery_time?'background:#2979ff;color:#fff;':'background:none;color:#333;'" @click="selectDeliveryTime(index,i,dIndex,dItem)" v-for="(dItem,dIndex) in deliveryTime" :key="'delivery-'+dIndex">{{dItem.text}}</label>
						</uni-td>
						<uni-td style="border:1px solid #EBEEF5;border-left:none;border-top:none;"><input placeholder="Please enter the price" @blur="checkPriceNumber" type="number"  :focus="focus=='param_price'?true:false" v-model="param[index].sku[i].en_price"/></uni-td>
					</uni-tr>
					
				
				</uni-table>
				
				<view class="goods-param-more left clearfix" style="margin-top:20px;height:300px;">
					<zmt-table-editor :index="{index:index,skuIndex:i}" :initTableData="param[index].sku[i].en_item" @htmlContentChange="getTableData"></zmt-table-editor>
				</view>
				</view>
				<!-- <view @click="addSku(index,param[index].sku.length -1)"  class="goods-add-param left" style="width:200px;margin-left:calc((100% - 200px)/2);background:#fff;border:1px solid #eee;color:#666;cursor: pointer;">增加SKU</view> -->
				
			
			</view>
		</view>
		
		
		<!-- <view class="goods-add-param left" style="width: 300px;margin-left:calc((100% - 250px)/2);cursor:pointer;" @click="alertParam">增加销售商品</view> -->
		
		
		<view class="goods-add-submit left clearfix">
			<label v-if="saveData && saveData.text">{{saveData.text}}</label>
			<label v-else style="cursor:default;">自动保存至草稿箱</label>
			<button @click="this.$refs.draftAdd.open();">草稿箱({{draftCount}})</button>
			<button @click="navigateTo()">取消</button>
			<button style="background:#2979ff;color:#fff;font-size:13px;line-height:45px;" @click="submitForm(1)" v-if="!goodsId">添加返回列表</button>
			<button style="background:#2979ff;color:#fff;font-size:13px;line-height:45px;" @click="submitForm(2)" v-if="!goodsId">添加后继续</button>
			<button style="background:#2979ff;color:#fff;font-size:13px;line-height:45px;" @click="submitForm(1)" v-if="goodsId">确定修改</button>
		</view>
		
		
		<uni-popup type="center" ref="draftAdd">
			<view class="goods-draft-alert left clearfix">
				<view class="goods-draft-title left clearfix">
					草稿箱
					<label @click="this.$refs.draftAdd.close()"><uni-icons type="closeempty" color="#666" size="20"></uni-icons></label>
				</view>
				<view class="goods-draft-content left clearfix">
					<view class="goods-draft-item left" v-for="(item,index) in draftData">
						<label style="margin-right:10px;margin-left:20px;">{{index + 1}}.</label>{{item.title}} <label class="right" style="margin-right:20px;font-size:14px;color:#2979ff;" @click="removeDraftById(item,index)">删除</label> <label class="right" style="margin-right:20px;font-size:14px;color:#2979ff;" @click="setEditDraft(item,index)">编辑</label>
					</view>
				</view>
			</view>
		</uni-popup>
		
	</view>
</template>

<script>
	var db = uniCloud.database();
import form from '../../uni_modules/uview-plus/components/u-form/form';
var aliyun = uniCloud.importObject('aliyun',{customUI:true});
	import common from '/static/js/common.js';
	function isPositiveInteger(value) {
	    return /^\+?[1-9]\d*$/.test(value);
	}
	
	function removeLastPipe(str) {
	  return str.replace(/\|(?=[^|]*$)/, '');
	}
	
	export default {
		name:"zh-goods-add-label",
		data() {
			return {
				paramHeight:900,
				deliveryTimeVal:'48 hours',
				categoryVal:'',
				deliveryTime:[
					{
						value: '48 hours',
						text: "48小时"
					},
					{
						value: 'within a week',
						text: "一周内"
					},
					{
						value: 'within 15 days',
						text: "15天内"
					},
					{
						value: 'within a month',
						text: "一月内"
					}
				],
				isPoster:false,
				a3d_file:null,
				pdf_file:null,
				step_file:null,
				category:[],
				feature:'',//产品特色
				focus:'',
				paramFeature:'',//销售属性的特色
				param:[
					 {
					      "en_title": "",
					      "en_goods_image": [],
					      "en_feature": null,
					      "en_content": "",
					      "en_goods_example_image": "",
					      "sku": [{
							  	  "en_model": "",
							  	  "en_serial_number": "",
							  	  "en_spec": "",
							  	  "en_price": null,
							  	  "en_item": "",
							  	  "en_zh_pdf": {},
							  	  "en_delivery_time": "within 48 hours",
							  	  "en_zh_3d": {},
							  	  "number":1
							  }]
					    }
				],
				formData:{
					en_category:[],
					en_images:[],
					category_id:[],
					en_poster:'',//封面图片
					en_spec:'',//规格参数
					en_describe:'',//技术说明
					en_title:'',//标题
					param:[{
					      "en_title": "",
					      "en_goods_image": [],
					      "en_feature": null,
					      "en_content": "",
					      "en_goods_example_image": "",
					      "sku": [{
							  	  "en_model": "",
							  	  "en_serial_number": "",
							  	  "en_spec": "",
							  	  "en_price": null,
							  	  "en_item": "",
							  	  "en_zh_pdf": {},
							  	  "en_delivery_time": "within 48 hours",
							  	  "en_zh_3d": {},
							  	  "number":1
							  }]
					    }]
					
				},
				draftId:null,
				saveData:{},
				draftCount:0,
				draftData:[],
				goodsId:''
			};
		},
		created() {
		  this.getCategory();
		  //只保留最近3天的草稿箱
		  this.removeExpiredDraft();
		},
		mounted() {
			//自动保存到草稿箱
			this.autoSaveToDraft();
			//查询出草稿箱的内容
			this.getDraft();
			setTimeout(()=>{
				
				if(this.goods && this.goods._id){
					this.setFormData(this.goods);
				}
				
			},500);
		},
		props:{
			userinfo:{
				type:Object,
				default:{}
			},
			goods:{
				type:Object,
				default:{}
			}
		},
		watch:{
			"goods"(val){
				if(val){
					if(val && val._id){
						this.setFormData(val);
						this.goodsId = val._id;
					}
					
				}
			}
		},
		methods:{
			//翻译
			translation(){
				uni.showLoading({
					title:'翻译中'
				})
				//先查询出中文
				db.collection('goods').where({_id:this.goodsId}).get({getOne:true}).then(res=>{
					var goods = res.result.data;
					delete goods._id;
					delete goods.images;
					delete goods.step_file;
					delete goods.category;
					delete goods.category_id;
					delete goods.is_shelf;
					delete goods.create_date;
					delete goods.sales;
					delete goods.page_view;
					delete goods.poster;
					delete goods.create_uid;
					delete goods.param;
					var data = JSON.stringify(goods);
					aliyun.getEnglish(data).then(ares=>{
						uni.hideLoading()
						var content = ares.result.data.choices[0].message.content;
						var contentData = JSON.parse(content);
						console.log('翻译结果',contentData);
						this.formData.en_title = contentData.title;
						this.formData.en_spec = contentData.spec;
						this.formData.en_describe = contentData.describe;
						this.formData.en_example_image = contentData.example_image;
						console.log('formData内容',this.formData);
					}).catch(aerr=>{
						uni.hideLoading()
						console.log('翻译出错',aerr);
					});
				});
			},
			//选中发货时间
			selectDeliveryTime(index,i,dIndex,dItem){
				this.param[index].sku[i].en_delivery_time = dItem.value;
			},
			//验证价格输入
			checkPriceNumber(index,i){
				var value = this.param[index].sku[i].price;
				var result = this.isPositiveInteger(parseInt(value));
				if(result == false){
					uni.showToast({
						title:'价格只能输入大于等于0的整数',
						icon:'none'
					})
					this.param[index].sku[i].price = '';
				}
			},
			//根据id删除草稿箱
			removeDraftById(item,index){
				db.collection('draft').where({_id:item._id}).remove().then(res=>{
					this.draftData.splice(index,1);
					this.getDraft();
				});
			},
			//清除过期草稿箱
			removeExpiredDraft(){
				const now = new Date().getTime();
				const threeDaysAgo = now - 7 * 24 * 60 * 60 * 1000;
				db.collection('draft').where({
					create_date:db.command.lt(threeDaysAgo)
				}).remove();
			},
			//编辑草稿箱内的内容
			setEditDraft(item,index){
				var that = this;
				uni.showModal({
					title: '提示',
					content: '编辑会清空当前页面内容，确定要编辑吗？',
					success: function (res) {
						if (res.confirm) {
							that.$refs.draftAdd.close();
							that.scrollView('goods_category');
							that.setFormData(item);
						}
					}
				});
			},
			//查询出草稿箱的内容
			getDraft(){
				db.collection('draft').where({create_uid:this.userinfo._id}).get({getCount:true}).then(res=>{
					this.draftCount = res.result.count;
					this.draftData = res.result.data;
				});
			},
			//自动保存到草稿箱
			autoSaveToDraft(){
				var that = this;
					var interval = setInterval(function(){
						if(that.formData.category && that.formData.category[0] && that.formData.title){
							that.submitFormDraft();
						}
					},60000);
				
			},
			//删除属性
			removeParam(index){
				var that = this;
				uni.showModal({
					title: '提示',
					content: '确定要删除吗？',
					success: function (res) {
						if (res.confirm) {
							that.param.splice(index,1);
						}
					}
				});
				
			},
			//删除sku
			removeSku(index,i){
				var that = this;
				uni.showModal({
					title: '提示',
					content: '确定要删除吗？',
					success: function (res) {
						if (res.confirm) {
							that.param[index].sku.splice(i,1);
						}
					}
				});
				
			},
			//获取默认值并赋值
			setFormData(goods){
				var that = this;
				if(goods && goods._id){
					that.formData = goods;
					if(goods && goods.category_id){
						that.categoryVal = goods.category_id[goods.category_id.length -1];
						that.deliveryTimeVal = goods.param[0].en_delivery_time;
					}
					
					if(goods.step_file){
						that.step_file = {
							path:goods.step_file,
							fileID:goods.step_file,
							url:goods.step_file,
							name:'已上传step文档'
						};
					}
					
					
					var param = goods.param;
					if(param && param.length > 0){
						for(var i = 0;i < param.length; i++){
							if(Array.isArray(param[i].en_feature)){
								var feature = param[i].en_feature.join('|\n') + '|';
								param[i].en_feature = removeLastPipe(feature);
							}
							
						}
					}
					
					that.param = param;
					
					if(goods.poster){
						this.isPoster = true;
					}
				}
			},
			//实时获取表格数据
			getTableData(e){
				var index = e.index;
				var html = e.html;
				this.param[index.index].sku[index.skuIndex].en_item = html;
			},
			//上传文件
			uploadFile(item,index,i){
				var that = this;
				
					if(item == 'zhpdf'){
						var extension = ['.pdf'];
					}else if(item == 'zh3d'){
						var extension = ['.step','.igs','x_t','x_b'];
					}
					uni.chooseFile({
					  count: 1,
					  extension:extension,
					  success(res) {
						  uni.showLoading({
						  	title:'上传中'
						  })
						  var tempFiles = res.tempFiles[0];
						  var title = tempFiles.name;
						  var filePath = res.tempFilePaths[0];
						 var size = tempFiles.size;
						  var fileName = common.generateUUIDWithExtension(tempFiles.name);
						  uniCloud.uploadFile({
						          filePath: filePath,
						          cloudPath: fileName,
						          onUploadProgress: function(progressEvent) {
						            var percentCompleted = Math.round(
						              (progressEvent.loaded * 100) / progressEvent.total
						            );
						  		  
						  		  that.uploadItem = item;
								  if(item == 'zhpdf'){
									  that.zhpdfPercent = percentCompleted;
								  }else if(item == 'zh3d'){
									  that.zhstepPercent = percentCompleted;
								  }
						          },
						          success(res) {
						  			var filePath = res.fileID;
										if(item == 'zhpdf'){
											that.param[index].sku[i].en_zh_pdf.title = title;
											that.param[index].sku[i].en_zh_pdf.url = filePath;
										}else if(item == 'zh3d'){
											that.param[index].sku[i].en_zh_3d.title = title;
												that.param[index].sku[i].en_zh_3d.url = filePath;
											}
											
									uni.hideLoading();
						  		}
						        });
					}
					})
				},
			//添加sku
			addSku(index,i){
				var param = this.param[index];
				if(!param.sku || !param.sku[i] || !param.sku[i].en_model){
					uni.showToast({
						title:'请填写型号',
						icon:'none'
					})
					return false;
				}
				if(!param.sku || !param.sku[i] || !param.sku[i].en_price){
					uni.showToast({
						title:'请填写价格',
						icon:'none'
					})
					return false;
				}
				
				this.param[index].sku.push({
					  "en_model": "",
					  "en_serial_number": "",
					  "en_spec": "",
					  "en_price": null,
					  "en_item": "",
					  "en_zh_pdf": {},
					  "en_delivery_time": "within 48 hours",
					  "en_zh_3d": {},
					  "number":1
				});
			},
			//上传pdf文档
			uploadPdfSuccess(e){
				var tempFiles = e.tempFiles[0];
				var tempFilePaths = e.tempFilePaths[0];
				this.paramData.en_zh_pdf = {
					url:tempFilePaths,
					title:tempFiles.name
				};
			},
			//上传3d文档
			upload3dSuccess(e){
				var tempFiles = e.tempFiles[0];
				var tempFilePaths = e.tempFilePaths[0];
				this.paramData.en_zh_3d = {
					url:tempFilePaths,
					title:tempFiles.name
				};
			},
			uploadStepSuccess(e){
				var tempFilePaths = e.tempFilePaths[0];
				var tempFiles = e.tempFiles;
				this.formData.step_file = tempFiles;
			},
			//提交表单
			submitForm(index){
				var formData = this.formData;
				if(formData._id){
					var id = formData._id;
					delete formData._id;
				}else if(this.goodsId){
					var id = this.goodsId;
				}
				formData.create_uid = this.userinfo._id;
				
				
				
				if(!formData.en_title){
					uni.showToast({
						title:'请输入产品标题',
						icon:'none'
					})
					this.focus = 'goods_title';
					this.scrollView('goods_title');
					return false;
				}
				
				
				// if(!formData.en_poster){
				// 	uni.showToast({
				// 		title:'请上传封面',
				// 		icon:'none'
				// 	})
				// 	this.focus = 'goods_poster';
				// 	this.scrollView('goods_poster');
				// 	return false;
				// }
				
				// if(this.isPoster == false){
				// 	uni.showToast({
				// 		title:'封面尺寸必须为1920px*540px,请修改',
				// 		icon:'none'
				// 	})
				// 	this.focus = 'goods_poster';
				// 	this.scrollView('goods_poster');
				// 	return false;
				// }
				
				
				
				
				// if(formData.en_images.length == 0){
				// 	uni.showToast({
				// 		title:'上传产品主图',
				// 		icon:'none'
				// 	})
				// 	this.focus = 'goods_image';
				// 	this.scrollView('goods_image');
				// 	return;
				// }
				
				
				formData.param = this.param;
				if(formData.param && formData.param.length > 0){
					for(var i = 0;i < formData.param.length;i ++){
						formData.param[i].en_delivery_time = this.deliveryTimeVal;
						if(formData.param && formData.param[i] && formData.param[i].en_feature){
							formData.param[i].en_feature = formData.param[i].en_feature.replace(/\n/g, '').split('|');
						}
					}
				}
				
				uni.showLoading({
					title:'添加中'
				})
				
				if(!id){
					db.collection('goods').add(formData).then(res=>{
						//删除草稿箱
						this.removeDraft();
						uni.hideLoading();
						uni.showToast({
							title:'添加成功'
						})
						
						setTimeout(function(){
							if(index == 1){
								uni.navigateTo({
									url:'/pages/index/index?page=goods-list'
								})
							}else if(index == 2){
								 uni.navigateTo({
								 	url:'/pages/index/index?page=goods-add'
								 })
							}
						},2000);
						
						
					});
				}else{
					db.collection('goods').where({_id:id}).update(formData).then(res=>{
						this.removeDraft();
						uni.hideLoading();
						uni.showToast({
							title:'修改成功'
						})
						setTimeout(function(){
							uni.navigateTo({
								url:'/pages/index/index?page=goods-list'
							})
						},2000);
						
					});
				}
				
			},
			//自动删除草稿箱
			removeDraft(){
				var id = this.draftId;
				db.collection('draft').doc(id).remove().then(res=>{
					console.log('删除结果',res);
				});
			},
			//保存到草稿箱
			submitFormDraft(){
				var formData = this.formData;
				if(formData._id){
					delete formData._id;
				}
				formData.create_uid = this.userinfo._id;
				formData.param = this.param;
				if(formData.param && formData.param.length > 0){
					for(var i = 0;i < formData.param.length;i ++){
						formData.param[i].en_delivery_time = this.deliveryTimeVal;
						
					}
				}
				var id = this.draftId;
				var time = new Date().getTime();
				if(!id){
					db.collection('draft').add(formData).then(res=>{
						this.draftId = res.result.id;
						this.getDraft();
						this.saveData = {
							time:time,
							text:this.timestampToTime(time)+'已自动保存到草稿箱'
						}
					});
				}else{
					db.collection('draft').where({_id:id}).update(formData).then(res=>{
						this.saveData = {
							time:time,
							text:this.timestampToTime(time)+'已自动保存到草稿箱'
						}
					})
				}
				
			},
			//转换时间戳
			timestampToTime(timestampMs) {
			  const date = new Date(timestampMs);
			  const hours = String(date.getHours()).padStart(2, '0');
			  const minutes = String(date.getMinutes()).padStart(2, '0');
			  return `${hours}:${minutes}`;
			},
			//滚动到对应的位置
			scrollView(id){
				const targetElement = document.getElementById(id);
				
				// 滚动到该元素位置
				targetElement.scrollIntoView({
				  behavior: 'smooth',  // 平滑滚动（可选）
				  block: 'end'       // 对齐方式：'start', 'center', 'end', 'nearest'
				});
			},
			//跳回列表页
			navigateTo(){
				uni.navigateTo({
					url:'/pages/index/index?page=goods-list'
				})
			},
			//产品分类发生变化时
			onchangeCategory(e){
				var value = e.detail.value;
				var category = [];
				var category_id = [];
				for(var i = 0;i<value.length;i++){
					category.push(value[i].text);
					category_id.push(value[i].value);
				}
				
				this.formData.category = category;
				this.formData.category_id = category_id;
			},
			//获取分类
			getCategory(){
				db.collection('category').where({is_show:true}).field('_id as value,en_title as text,children').orderBy('sort asc').get().then(res=>{
					var category = res.result.data;
					for(var i = 0;i<category.length;i++){
						var children = category[i].children;
						if(children && children[0]){
							for(var x = 0;x < children.length;x++){
								var value = children[x]._id;
								var text = children[x].en_title;
								category[i].children[x] = {
									value:value,
									text:text
								}
							}
						}
						
					}
					this.category = category;
				});
			},
			//上传产品示意图
			uploadExampleImage(e){
				this.param[e.index].en_goods_example_image = e.imageUrl[0];
			},
			//上传产品属性主图后
			uploadZhuImage(e){
				this.param[e.index].en_goods_image= e.imageUrl;
			},
			
			//上传产品主图后
			uploadZhuImages(e){
				this.formData.images = e;
			},
			//编辑器内容发生变化时
			handleContentChange(e){
				this.formData.en_spec = e[0].html;
				this.formData.en_describe = e[1].html;
				this.formData.en_example_image = e[2].html;
			},
			
			//属性内的富文本编辑器变化时
			getParamContent(e){
				this.paramData.item = e.html;
			},
			//上传主图
			uploadParamImage(e){
				this.paramData.en_goods_image = e;
			},
			//上传示意图
			uploadParamExampleImage(e){
				this.paramData.en_goods_example_image = e[0];
			},
			//弹出添加属性弹窗
			alertParam(){
				this.param.push({
					      "en_title": "",
					      "en_goods_image": [],
					      "en_feature": [],
					      "en_content": "",
					      "en_goods_example_image": "",
					      "sku": [{
								  "en_model": "",
								  "en_serial_number": "",
								  "en_spec": "",
								  "en_price": null,
								  "en_item": "",
								  "en_zh_pdf": {},
								  "en_delivery_time": "within 48 hours",
								  "en_zh_3d": {},
								  "number":1
							}]
					    });
			},
			//上传banner图
			uploadBannerImage(e){
				var that = this;
				 uni.getImageInfo({
				      src: e.fileID,
				      success: (info) => {
						if(info.width!=1920 || info.height!=540){
							that.isPoster = false;
							uni.showToast({
								title:'封面尺寸必须为1920*540px',
								icon:'none'
							})
							that.focus = 'goods_poster';
							that.scrollView('goods_poster');
							return;
						}else{
							that.isPoster = true;
							that.formData.en_poster = e.fileID;
						}
				      },
				      fail: (err) => {
				        console.error('获取图片信息失败:', err);
				      }
				    });
					
				
			}
		}
	}
</script>

<style>
	.tiptap,.ProseMirror,.aie-content{height:250px !important;}
	.goods-param-contents table {
	    border-collapse: collapse; /* 关键：合并边框，避免双线 */
	    width: 100%; /* 可选：设置宽度 */
	    font-family: Arial, sans-serif;
	}
	
	.goods-param-contents table, th, td {
	    border-right: 1px solid #ebeef5; /* 统一设置边框，颜色为浅灰色 */
		border-bottom:1px solid #ebeef5;
		font-size:14px;
	}
	.goods-param-contents th:first-child, td:first-child{
		border-left:1px solid #ebeef5;
	}
	.goods-param-contents th{border-top:1px solid #ebeef5;}
	.goods-param-contents th {
	    background-color: #fff; /* 表头背景色，提升可读性 */
	    padding: 10px;
	    text-align: left;
		font-weight:400;
	}
	
	.goods-param-contents td {
	    padding: 8px;
	    background-color: #ffffff;
	}
	
	/* 鼠标悬停效果（可选） */
	.goods-param-contents tr:hover {
	    background-color: #f9f9f9;
	}
	.uni-input-input{
		text-align: left !important;
		font-weight:400 !important;
		background:#fff !important;
		font-size:14px !important;
	}
	tr{background:#fff !important;}
	.cell-content{background:#fff !important;}
</style>
<style scoped>
	.goods-param-time:hover{background:#2979ff;color:#fff;}
	.goods-param-time{padding-left:5px;padding-right:5px;line-height:25px;border:1px solid #ccc;margin-left:5px;margin-right:5px;font-size:13px;border-radius: 3px;}
	.goods-draft-title label{float:right;margin-right:10px;}
	.goods-draft-item{width:100%;border-bottom:1px solid #f5f5f5;height:40px;line-height:40px;}
	.goods-draft-content{width:100%;height:450px;overflow: auto;}
	.goods-draft-title{width:calc(100% - 20px);line-height:40px;height:40px;font-weight:500;margin-left:20px;}
	.goods-draft-alert{width:800px;height:500px;border:1px solid #eee;bottom:40px;left:200px;background:#fff;border-radius: 10px;}
	.goods-param-del{width:30px;height:30px;text-align: center;background:#fff;line-height:30px;border-radius: 50%;right:-10px;top:-10px;border:1px solid #eee;cursor:pointer;}
	.param-upload-box{margin-right:10px;background:#2979ff;color:#fff;font-size:12px;padding:5px;width:80px;border-radius: 5px;text-align: center;float:left;}
	.goods-upload-left{line-height:25px;margin-left:20px;margin-top:5px;}
	.goods-upload-left{width:47.5%;height:120px;border-right:1px solid #ebeef5;}
	.goods-alert-upload{width:100%;height:130px;background:#f8f8f8;border-radius: 10px;margin-top:20px;border:1px solid #ebeef5;}
	.goods-add-submit label:first-child{margin-left:calc((100% - 1000px)/2);float:left;line-height:45px;font-size:13px;color:rgb(66, 185, 131);}
	.goods-add-submit button{width:150px;float:left;margin-left:20px;margin-right:20px;}
	.goods-add-submit{width:100%;height:50px;margin-top:100px;}
	.goods-fenlei-content{width:100%;margin-top:20px;}
	.goods-image-top li{float:left;line-height:20px;width:calc(100% - 20px);padding-left:10px;margin-top:10px;}
	.goods-param-add label{float:right;width:70px;height:40px;background:#2979ff;margin-right:20px;line-height:40px;text-align: center;margin-top:5px;border-radius: 5px;color:#fff;}
	.goods-param-add{width:100%;line-height:50px;height:50px;margin-top:10px;}
	.goods-param-content{width:calc(100% - 20px);min-height:500px;padding-left:10px;padding-right:10px;}
	.goods-add-param{width:50%;margin-left:25%;height:40px;background:#2979ff;color:#fff;text-align: center;line-height:40px;margin-top:30px;border-radius: 20px;font-size:13px;}
	input{font-size:14px;}
	.goods-param-more{width:100%;height:400px;margin-top:20px;}
	.goods-param-box{width:1060px;margin-left:calc((100% - 1040px)/2);min-height:200px;box-shadow: 1px 1px 1px 2px rgba(0, 0, 0, 0.2);margin-top:20px;border-radius: 5px;padding:10px;}
	.goods-image-top textarea{width:calc(100% - 20px);padding:10px;height:calc(100% - 20px);font-size:13px;}
	.goods-image-top{width:calc(100% - 10px);height:120px;border:1px solid #ddd;border-radius: 3px;}
	.goods-image-content{width:420px;height:300px;margin-top:10px;margin-left:10px;}
	.goods-image-left{width:300px;height:300px;margin-top:10px;}
	.goods-image{width:100%;height:330px;margin-top:10px;}
	.goods-title input{width:calc(100% - 10px);height:50px;line-height:50px;border:1px solid #ddd;padding-left:10px;border-radius: 3px;}
	.goods-title{width:100%;height:50px;}
	.goods-box{width:1040px;margin-left:calc((100% - 1040px)/2);min-height:380px;margin-top:20px;box-shadow: 1px 1px 1px 2px rgba(0, 0, 0, 0.2);padding:20px;border-radius: 5px;}
	.label-box{width:1040px;margin-left:calc((100% - 1040px)/2);margin-top:20px;box-shadow: 1px 1px 1px 2px rgba(0, 0, 0, 0.2);padding:20px;border-radius: 5px;}
	.menu-banner{width:1040px;height:350px;margin-left:calc((100% - 1040px)/2);box-shadow: 1px 1px 1px 2px rgba(0, 0, 0, 0.2);padding:20px;border-radius: 5px;}
	
</style>